<template>
<view class="p_10">
    <view class="f_s_24 f_w_b">上传附件简历</view>
    <view class="m_t_10 c_9 m_b_20 f_s_14">
        将文件发送至微信（文件传输助手、自己或好友），在本页面点
        击上传按钮，选择发送文件的聊天对象，选译文件，点击确认即
        可上传成功。
    </view>
    <u-swiper :list="banner" indicator :height="bannerHeight"  imgMode="heightFix" radius="0" indicatorActiveColor="#47C156" indicatorInactiveColor="#dddddd"></u-swiper>
    <view class="m_t_100">
        <u-button type="primary" @click="pdfShow=true">上传附件简历</u-button>
    </view>
    <!-- 上传简历 -->
    <u-modal :show="pdfShow" title="上传简历" :showCancelButton="true" @confirm="handlePdfConfirm" @cancel="handlePdfCancel">
        <view class="dis_f_co_c_c">
            <u-upload
                :fileList="fileList1"
                @afterRead="afterRead"
                @delete="deletePic"
                capture="album"
                accept="file"
                name="1"
                multiple
                :maxCount="1"
            ></u-upload>
            <view class="c_9 m_t_5 f_s_14">请上传简历</view>
        </view>
    </u-modal>
</view>
</template>
<script>
import {getByKeyApi, getMyInfoApi, uploadResumeApi} from "@/sheep/api";
import {baseUrl} from "@/sheep/request/baseUrl";
export default {
    name: "index",
    data() {
        return {
            banner: [],
            bannerHeight: 0,
            pdfShow:false,
            fileList1:[],
            myInfoVo:{}
        }
    },
    onLoad(params) {
        this.getBannerList();
        this.getBannerHeight();
        this.getMyInfo();
    },
    methods: {
        async getBannerList() {
            let res = await getByKeyApi("uploadTips");
            this.banner = res.data;
        },
        async getBannerHeight() {
            let res = await getByKeyApi("uploadTipsHight");
            this.bannerHeight = res.data[0]?.configValue;
        },

        handlePdfCancel(){
            this.pdfShow=false
        },
        getMyInfo() {
            getMyInfoApi().then((res) => {
                this.myInfoVo = res.data;
            });
        },
        handlePdfConfirm(){
            let pdf = ''
            if (this.fileList1.length==0){
                uni.showToast({
                    title: '请上传简历',
                    icon: 'none'
                })
                return
            }
            if (Array.isArray(this.fileList1)) {
                if (this.fileList1.length > 0) {
                    this.fileList1.forEach((item, index) => {
                        if (index === 0) {
                            pdf = item.ossId;
                        } else {
                            pdf = pdf + "," + item.ossId;
                        }
                    });
                } else {
                    pdf = "";
                }
            }
            uploadResumeApi({pdf:pdf,userId:this.myInfoVo.userId}).then(res => {
                uni.showToast({
                    title: res.msg,
                    icon: 'none'
                })
                this.pdfShow = false
                this.getMyInfo()
            })
        },

        // 上传图片
        async afterRead(event) {
            let lists = [].concat(event.file);
            console.log("lists", lists);
            console.log(" this[`fileList${event.name}`]",  this[`fileList${event.name}`]);
            let fileListLen = this[`fileList${event.name}`].length;
            console.log("fileListLen", fileListLen);
            lists.map((item, index) => {
                this[`fileList${event.name}`].push({
                    ...item,
                    status: "uploading",
                    message: "上传中",
                });
            });
            for (let i = 0; i < lists.length; i++) {
                const result = await this.uploadFilePromise(lists[i].url, lists, event);
                console.log("result", result);
                let item = this[`fileList${event.name}`][fileListLen];
                this[`fileList${event.name}`].splice(
                    fileListLen,
                    1,
                    Object.assign(item, {
                        status: "success",
                        message: "",
                        url: result.url,
                        name: result.fileName,
                        ossId: result.ossId
                    })
                );
                fileListLen++;
            }
        },
        uploadFilePromise(url, lists, event) {
            let token = uni.getStorageSync('token')
            let headerToken = "Bearer " + token
            return new Promise((resolve, reject) => {
                let a = uni.uploadFile({
                    url: baseUrl + "/resource/oss/upload",
                    filePath: url,
                    name: "file",
                    header: {
                        Authorization: headerToken,
                        clientId: "be7052a7e4f802c20df10a8d131adb12"
                    },
                    formData: {
                        user: "test",
                    },
                    success: (res) => {
                        let data = JSON.parse(res.data);
                        console.log("data", data);
                        if (data.code == 200) {
                            resolve(data.data);
                        } else {
                            if (lists.length > 0) {
                                lists.map((item, index) => {
                                    this[`fileList${event.name}`].splice(index, 1)
                                });
                            }
                            uni.showToast({
                                title: data.msg,
                                icon: "none",
                            });
                        }
                    },
                });
            });
        },
        // 删除图片
        deletePic(event) {
            this[`fileList${event.name}`].splice(event.index, 1);
        },
    }
}
</script>

<style scoped lang="scss">

</style>
